---@class DarkStreetMgr
DarkStreetMgr = {}
local this = DarkStreetMgr
function DarkStreetMgr.init()
    this.broadcastList = {}
    this.attackMeName = ""
    this.lostCount = 0
    this.isOpen = false     --是否开启
    this.joinNum = 0        --参与次数
    this.startOrEndTime = 0
    this.isMatchSuc = false --匹配成功
    this.resultData = {
        killNum = 0,    --总击杀人数
        money = 0,      --获得资源
        killStreak = 0, --最高连胜
    }
    this.resCountInWeek = 0
    this.resCompensationInWeek = 0
    this.all_money = 0
    this.resItemModelId = CurrencyEnum.GOLD
    ListenEvent(RedRob.Config.Event.BattleReady,this.BattleReady)
    ListenEvent(RedRob.Config.Event.BattleLeave,this.BattleLeave)
    this.battleBeforeResCount = 0
end
function DarkStreetMgr.OnDestroy()
    this.broadcastList = nil
   this.Clear()
end
function DarkStreetMgr.Clear()
    this.broadcastList = {}
end
function DarkStreetMgr.SetResult(killNum,money,killStreak)
    this.resultData.killNum = killNum
    this.resultData.money = money
    this.resultData.killStreak = killStreak
    this.isInResult = true
    local old = this.battleBeforeResCount
    local _,_,_,limit = this.GetWeekLimitData()
    local total =  math.min(this.battleBeforeResCount + money,limit)
    this.resultData.realGetMoney = math.max(total - old,0)
end
---@param winPlayer RedRob.Player
---@param failedPlayer RedRob.Player
function DarkStreetMgr.AddBroadcastMsg(winPlayer,failedPlayer,resCount)
    resCount = math.abs(resCount)
    local main = RedRob.KeyController.GetCtrl()
    if main then
        if main == failedPlayer then
            this.attackMeName = winPlayer:GetName()
            this.lostCount = resCount
        end
    end
    local tb = { winId = winPlayer:GetId(),failedId = failedPlayer:GetId(),count = resCount}
    table.insert(this.broadcastList,tb)
end
function DarkStreetMgr.GetBroadcastMsg()
    if #this.broadcastList > 0 then
        return table.remove(this.broadcastList,1)
    end
    return nil
end
---剩余次数
function DarkStreetMgr.GetRemainTimes()
    return 1
end
---剩余购买次数
function DarkStreetMgr.GetRemainBuyTimes()
    return 10
end
function DarkStreetMgr.GetOpenTime()
    return logicMgr.CronMgr.GetNextDateTime(dataTable.getGlobalString(340),HelperFunc.GetServerTime())
end
function DarkStreetMgr.GetEndTime()
    return logicMgr.CronMgr.GetNextDateTime(dataTable.getGlobalString(341),HelperFunc.GetServerTime())
end
function DarkStreetMgr.CanEnter()
    if not this.CheckOpen() then
        DarkStreetHandler.ReqDarkInfo(DarkStreetMgr.DarkInfoCallback)
    else
        this.DarkInfoCallback()
    end
end
function DarkStreetMgr.DarkInfoCallback()
    if this.CheckOpen(true) then
        showUI(PanelNames.UIBattleTeam,EnumConst.FightTypeConst.DarkStreet,nil,function(formations)
            if this.GetRemainTimes() == 0 then
                UISysTips.AddMsg(getLanguage("ErrorCode_172108"))
                return
            end
            this.formations = formations
            if not this.IsOpen() then
                UISysTips.AddMsg(getLanguage("ClimbTowerTips5")) -- 未到开启时间
                return
            end
            logicMgr.UIShowManager.ShowCommonConfirm(getLanguage("DarkLaneTipes_19"),getLanguage("DarkLaneTipes_20"),DarkStreetMgr.ConfirmEnter)
            logicMgr.DarkStreetMgr.Track_clickEnter()
        end)
        --showUI(PanelNames.UIEnterDarkStreet)
    end
end

function DarkStreetMgr.ConfirmEnter()
    local form = {}
    for k,v in pairs(this.formations[1]) do
        table.insert(form,k,v)
    end
    this.battleBeforeResCount = this.resCountInWeek
    logicMgr.FormationManager.SetFormation(EnumConst.FormationType.DarkStreet, form)
    --[[local stage = this.GetOpenStage()
    if stage ~= RedRob.Config.OpenStage.open then
        DarkStreetHandler.ReqDarkApply(true,form)
    end
    RedRob.ClientBattle.Start(1,7,5)]]
    DarkStreetHandler.ReqDarkEntry(form)
end
function DarkStreetMgr.CheckOpen(showTip)
    local isOpen = this.IsOpen()
    if not isOpen  then
        if not showTip then
            return false
        end
        UISysTips.AddMsg(getLanguage("ClimbTowerTips5")) -- 未到开启时间
        return false
    end
    return true
end
function DarkStreetMgr.GetWeekLimitData()
    local dataList = decodeJsonStrToTabel(dataTable.getGlobalString(359))
    local mapId  = logicMgr.MapManager.getMapId()
    for k,v in pairs(dataList) do
        if mapId >= v[1] and mapId <= v[2] then
            return v[1],v[2],v[3],v[4],k
        end
    end
    return 0,0,0,0,0
end
function DarkStreetMgr.IsOpen()
    local stage = this.GetOpenStage()
    if stage ~= RedRob.Config.OpenStage.close then
        return true
    end
    --[[local open = this.GetOpenTime()
    local _end = this.GetEndTime()
    if time >= open and time <_end then
        return true
    end
    if open >_end and time < _end then
        return true
    end]]
    return false
end
function DarkStreetMgr.GetOpenStage()
    local OpenStage = RedRob.Config.OpenStage
    local _end = this.GetEndTime()
    local time =  HelperFunc.GetServerTime()
    if this.isOpen and time < this.startOrEndTime then
        return OpenStage.open,_end - time
    end
    local hour = tonumber(os.date("%H",_end))
    local minute = tonumber(os.date("%M",_end))
    if hour == 22 and minute == 35 then--去除10点半的一场
        local nextTime = tonumber(logicMgr.CronMgr.GetNextDateTime(dataTable.getGlobalString(341),_end + 1))
        return OpenStage.close,nextTime-time
    end
    local open = this.GetOpenTime()
    local flag = false
    local stage = OpenStage.close
    local remainTime = 0
    if time >= open and time <_end then
        flag = true
    end
    if open >_end and time < _end then
        flag = true
    end
    if flag then
        stage = OpenStage.open
        remainTime = _end - time
    else
        remainTime = open - time
    end
    return stage,remainTime

end
function DarkStreetMgr.BattleLeave()
    this.Clear()
    closeUI(PanelNames.UIDarkStreetPlayerLayer)
    closeUI(PanelNames.UIDarkStreet)
    closeUI(PanelNames.UIDarkStreetCountDown)
    closeUI(PanelNames.UIDarkStreetDead)
end
function DarkStreetMgr.BattleReady(type)
    this.isInResult = false
    closeUI(PanelNames.UIBattleTeam)
    showUI(PanelNames.UIDarkStreetPlayerLayer)
    showUI(PanelNames.UIDarkStreet)
end

--- ------------------埋点相关 start-----------
function DarkStreetMgr.Track_clickEnter()
    -- TAManager.TrackByDictionary("darkstreet_click_enter", {room_id = 0})
end

function DarkStreetMgr.Track_enterSuccess()
    TAManager.TrackByDictionary("darkstreet_enter", {room_id = logicMgr.DarkStreetMgr.room_id})
end

function DarkStreetMgr.Track_clickExit()
    -- local step = logicMgr.DarkStreetMgr.isOpen and 1 or 0
    -- TAManager.TrackByDictionary("darkstreet_click_exit", {room_id = logicMgr.DarkStreetMgr.room_id, cur_step = step})
end

function DarkStreetMgr.Track_exitSuccess(id, reason)
    local step = logicMgr.DarkStreetMgr.isOpen and 1 or 0
    local all = decodeJsonStrToTabel(dataTable.getGlobalString(345))
    local number = all - this.joinNum
    TAManager.TrackByDictionary("darkstreet_exit", {room_id = logicMgr.DarkStreetMgr.room_id, cur_step = step, number = number, reason = reason})
end

function DarkStreetMgr.Track_activityStart()
    local dict  = {}
    dict.room_id = this.room_id
    dict.room_number = RedRob.ActorManager.GetPlayerCount()
    dict.own_battle_array = ""
    dict.own_support_skill = logicMgr.SkillManager.GetSelectSkillId()
    local _,_,_,_,index = logicMgr.DarkStreetMgr.GetWeekLimitData()
    dict.room_level = index

    local form = FormationManager.GetFormation(EnumConst.FormationType.DarkStreet)
    local h = nil
    for k,v in pairs(form) do
        h = HeroManager.getHeroDatabyPartnerId(v,true)
        if h then
            if dict.own_battle_array == "" then
                dict.own_battle_array = h.partnerModelId
            else
                dict.own_battle_array = table.concat({dict.own_battle_array, ",", h.partnerModelId})
            end
            dict["m_pos" .. k .."_hid"] = h.partnerModelId
        else
            dict["m_pos" .. k .."_hid"] = "0"
        end
    end

    TAManager.TrackByDictionary("darkstreet_start", dict)
end

function DarkStreetMgr.Track_battle(msg)
    DarkStreetMgr.all_money = DarkStreetMgr.all_money + msg.money
    local dic = {kill_streak = msg.killStreak}
    dic.money_number = DarkStreetMgr.all_money
    dic.money_delta = msg.money
    TAManager.TrackByDictionary("darkstreet_battle", dic)
end

function DarkStreetMgr.Track_mining(id)
    TAManager.TrackByDictionary("darkstreet_mining", {id = id})
end

--- ------------------埋点相关 end-----------


return DarkStreetMgr
